package org.wu.framework.lazy.database.datasource.proxy.handler;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLTimeoutException;

public abstract class AbstractDataSourceHandler implements DataSourceHandler {

    /**
     * <p>Attempts to establish a connection with the data source that
     * this {@code DataSource} object represents.
     *
     * @param dataSource
     * @return a connection to the data source
     * @throws SQLException        if a database access error occurs
     * @throws SQLTimeoutException when the driver has determined that the
     *                             timeout value specified by the {@code setLoginTimeout} method
     *                             has been exceeded and has at least tried to cancel the
     *                             current database connection attempt
     */
    @Override
    public Connection getLazyConnection(DataSource dataSource) throws SQLException {
        return doGetConnection(dataSource);

    }

    public abstract Connection doGetConnection(DataSource dataSource) throws SQLException;

    /**
     * <p>Attempts to establish a connection with the data source that
     * this {@code DataSource} object represents.
     *
     * @param dataSource
     * @param username   the database user on whose behalf the connection is
     *                   being made
     * @param password   the user's password
     * @return a connection to the data source
     * @throws SQLException        if a database access error occurs
     * @throws SQLTimeoutException when the driver has determined that the
     *                             timeout value specified by the {@code setLoginTimeout} method
     *                             has been exceeded and has at least tried to cancel the
     *                             current database connection attempt
     * @since 1.4
     */
    @Override
    public Connection getLazyConnection(DataSource dataSource, String username, String password) throws SQLException {
        return doGetConnection(dataSource, username, password);
    }

    protected abstract Connection doGetConnection(DataSource dataSource, String username, String password)
            throws SQLException;
}
